package com.laoyao.comm.config;



import javax.servlet.http.Cookie;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.laoyao.util.MoreTime;
import com.laoyao.util.TextUtil;

public class DelLanJie implements Interceptor{

	@Override
	public void intercept(Invocation inv) {
		System.out.println("删除拦截==================");
		// TODO 自动生成的方法存根
		Cookie name=TextUtil.getCookieByName(inv.getController().getRequest(), "username");
		Cookie pwd=TextUtil.getCookieByName(inv.getController().getRequest(), "password");
		String username="";
		String password="";
		if (name!=null) {
			username=name.getValue();
		}
		if (pwd!=null) {
			password=pwd.getValue();
		}
		//保存删除操作记录
		String str = inv.getController().getRequest().getQueryString();
		String id[] = inv.getController().getPara("id").split(",");   //删除的id，可能是多个
		String type[] = inv.getActionKey().split("/");   //删除的对象，字符串分割
		String temp = null;         //对象值
		String temp2 = "";         //对象值
		if(type!=null&&type.length>1){
			temp = type[1];
		}
		if(type!=null&&type.length>2){
			temp2 = type[2];
		}
		if (str != null &&!str.equals("null")) {
			if(temp!=null){
				Record r=new Record();
				r.set("record_name", username).set("record_ip", TextUtil.getIpAddr(inv.getController().getRequest())).set("record_time", MoreTime.getTime());
				if(temp.equals("hospital")){
					r.set("record_message", "删除了id为："+display(id)+",名称为："+find(id,"hospital","hospital_id","hospital_name")+"的"+temp);
				}else if(temp.equals("user")){
					if(temp2.equals("dietitianDel")){
						r.set("record_message", "删除了id为："+display(id)+",名称为："+find(id,"dietitian","dietitian_id","dietitian_username")+"的营养师");
					}else{
						r.set("record_message", "删除了id为："+display(id)+",名称为："+find(id,"user","user_id","user_name")+"的"+temp);
					}
				}else if(temp.equals("admin")){
					r.set("record_message", "删除了id为："+display(id)+",名称为："+find(id,"admin","admin_id","admin_name")+"的"+temp);
				}else if(temp.equals("menu")){
					r.set("record_message", "删除了id为："+display(id)+",名称为："+find(id,"menu","menu_id","menu_name")+"的"+temp);
				}else if(temp.equals("origin")){
					r.set("record_message", "删除了id为："+display(id)+",名称为："+find(id,"origin","origin_id","origin_name")+"的"+temp);
				}else {
					r.set("record_message", "无操作");
					
				}
				Db.save("record_del", r);
			}
		}else{
			if(temp!=null){
				Record r=new Record();
				r.set("record_name", username).set("record_ip", TextUtil.getIpAddr(inv.getController().getRequest())).set("record_time", MoreTime.getTime());
				if(temp.equals("hospital")){
					r.set("record_message", "删除了id为："+display(id)+",名称为："+find(id,"hospital","hospital_id","hospital_name")+"的"+temp);
				}else if(temp.equals("user")){
					if(temp2.equals("dietitianDel")){
						r.set("record_message", "删除了id为："+display(id)+",名称为："+find(id,"dietitian","dietitian_id","dietitian_username")+"的营养师");
					}else{
						r.set("record_message", "删除了id为："+display(id)+",名称为："+find(id,"user","user_id","user_name")+"的"+temp);
					}
				}else if(temp.equals("admin")){
					r.set("record_message", "删除了id为："+display(id)+",名称为："+find(id,"admin","admin_id","admin_name")+"的"+temp);
				}else if(temp.equals("menu")){
					r.set("record_message", "删除了id为："+display(id)+",名称为："+find(id,"menu","menu_id","menu_name")+"的"+temp);
				}else if(temp.equals("origin")){
					r.set("record_message", "删除了id为："+display(id)+",名称为："+find(id,"origin","origin_id","origin_name")+"的"+temp);
				}else {
					r.set("record_message", "无操作");
					
				}
				Db.save("record_del", r);
			}
		}
		if(TextUtil.isEmpty(username)==false && TextUtil.isEmpty(password)==false){
			String sql="select * from admin where admin_tel=? and admin_pwd=? and admin_state=0";
			Record user=Db.findFirst(sql,username,password);
			if (user!=null) {
				//拦截到权限授权提示页面
				inv.invoke();//用户已授权，放过拦截
			}else {
				//拦截到登录页面
				inv.getController().redirect("/login");
			}
		}else{  
			//拦截到登录页面
	        inv.getController().redirect("/login");
		}
	
	}
	
	//传入id数据，表，主键，和想要查询的字段名，返回字段值字段串
	public String find(String[] id,String table,String table_id,String col){
		String value = "";
		for (int i = 0; i < id.length; i++) {
			String buffer_id = id[i];
			Record buffer = Db.findById(table, table_id, buffer_id);
			if(buffer!=null){
				value += buffer.get(col) +",";
			}
		}
		if(value.length()!=0)
			value = value.substring(0, value.length()-1);
		return value;
	}
	
	// 传入id数据,返回字段串
	public String display(String[] id) {
		String value = "";
		for (int i = 0; i < id.length; i++) {
			String buffer_id = id[i];
			value += buffer_id + ",";
		}
		value = value.substring(0, value.length() - 1);
		return value;
	}

}
